package huixue.cn.com.rxapp.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.Collections;
import java.util.List;

import huixue.cn.com.rxapp.R;

/**
 * Created by gongdongyang on 2018/7/30.
 */

public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder> implements ItemTouchMoveListener{

    private Context context;

    private List<String> datas;

    private final LayoutInflater mInflater;


    public RecycleViewAdapter(Context context, List<String> datas) {
        this.context = context;
        this.datas = datas;
        mInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.iv_item.setText(datas.get(position));
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        Collections.swap(datas, fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
        return true;
    }

    @Override
    public boolean onItemRemove(int position) {
        datas.remove(position);
        notifyItemRemoved(position);
        return true;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView iv_item;
        public ViewHolder(View itemView) {
            super(itemView);
            iv_item = (TextView) itemView.findViewById(R.id.iv_item);
        }
    }

}


